<% unless @saved -%>
  TracksPages.show_errors(html_for_error_messages());

  function html_for_error_messages() {
    return "<%= js_error_messages_for(@project) %>";
  }
<% else -%>
  <% if @go_to_project -%>
    redirect_to("<%= project_path(@project) -%>")
  <% else -%>
  TracksPages.hide_errors();
  hide_empty_message();
  TracksPages.set_page_badge(<%= @down_count %>);
  update_active_projects_container();
  add_project();
  clear_form();
  TracksPages.page_inform("Created new project '<%= @project.name%>'");
  <% end -%>

/* TODO: make this generic for all pages with lists */
function hide_empty_message() {
  $('projects-empty-nd').hide();
}

/* TODO: refactor and move this to application.js */
function add_project() {
  $('#list-active-projects').append(html_for_project_listing());
}

/* TODO: refactor and move this to application.js */
function clear_form() {
  $('#project_form').clearForm();
  $('#project_form input:text:first').focus();
}

/* TODO: refactor and move this to application.js */
function update_active_projects_container() {
  ProjectListPage.set_state_container_visibility('active', true);
  ProjectListPage.update_state_count('active', <%=@active_projects_count%>);
}

function html_for_project_listing() {
  return "<%= @saved ? js_render('project_listing', {}, @project) : "" %>";
}

<% end -%>